﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;
using System.ComponentModel;
using System.IO;    
using System.Device.Location;



namespace client
{
    public class MobileClient : INotifyPropertyChanged
    {
        public string testName;
        public string latitude;
        public string longitude;
        GeoCoordinateWatcher watcher;
        public string coordinate;

        public MobileClient()
        {
            Console.WriteLine("fuccccck");
            testName = "bbb";
            latitude = "la";
            longitude = "lo";
        }
        
        public void GetResponse(string name) 
        {
            Console.Write("GetResponse");

            UriBuilder fullUri = new UriBuilder("http://csci5221group05test.cloudapp.net/WebForm1.aspx");
            fullUri.Query = "location="+name;

            // initialize a new WebRequest
            HttpWebRequest dataRequest = (HttpWebRequest)WebRequest.Create(fullUri.Uri);

            // set up the state object for the async request
            DataUpdateState dataState = new DataUpdateState();
            dataState.AsyncRequest = dataRequest;
            dataRequest.BeginGetResponse(new AsyncCallback(HandleDataResponse),
                dataState);


        }

        private void HandleDataResponse(IAsyncResult asyncResult) 
        {
            Console.Write("HandleDataResponse");
            // get the state information
            DataUpdateState dataState = (DataUpdateState)asyncResult.AsyncState;
            HttpWebRequest dataRequest = (HttpWebRequest)dataState.AsyncRequest;

            // end the async request
            dataState.AsyncResponse = (HttpWebResponse)dataRequest.EndGetResponse(asyncResult);

            Stream streamResult;
            streamResult = dataState.AsyncResponse.GetResponseStream();
            StreamReader sr = new StreamReader(streamResult);
            
            
           
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                testN = sr.ReadToEnd();
            });
        
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public string testN
        {
            get
            {
                return testName;
            }
            set
            {
                this.testName = value;
                NotifyPropertyChanged("testN");
            }
        }

        public string Latitude
        {
            get
            {
                return latitude;
            }
            set
            {
                this.latitude = value;
                NotifyPropertyChanged("Latitude");
            }
        }

        public string Longitude
        {
            get
            {
                return longitude;
            }
            set
            {
                this.longitude = value;
                NotifyPropertyChanged("Longitude");
            }
        }

        public void NotifyPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }



        public void GPSService() 
        {
            watcher = new GeoCoordinateWatcher();

            
            watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
            
            Console.Write("Gps not start ");

            watcher.Start();
            GeoCoordinate coord = watcher.Position.Location;
            if (coord.IsUnknown != true)
            {
                Console.WriteLine("Lat: {0}, Long: {1}",
                    coord.Latitude,
                    coord.Longitude);
            }
            else
            {
                Console.WriteLine("Unknown latitude and longitude.");
            }

            Console.Write("Gps start ");
  
        }




        void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() => MyPositionChanged(e));
        }

        void MyPositionChanged(GeoPositionChangedEventArgs<GeoCoordinate> e) 
        {
            //latitude = e.Position.Location.Latitude.ToString("0.000");
            //longitude = e.Position.Location.Longitude.ToString("0.000");

            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                Latitude = e.Position.Location.Latitude.ToString("0.000");
                Longitude = e.Position.Location.Longitude.ToString("0.000");
            });

            coordinate = e.Position.Location.Latitude.ToString("0.000") + "," + e.Position.Location.Longitude.ToString("0.000");

            GetResponse(coordinate);
            //GetResponse(Latitude + "," + Longitude);    
            watcher.Dispose();
        }



    }

    public class DataUpdateState
    {
        public HttpWebRequest AsyncRequest { get; set; }
        public HttpWebResponse AsyncResponse { get; set; }
    }
}
